/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargaRankDias.java
 *
 * Created on Feb 27, 2011, 5:37:44 PM
 */

package forms;

import beans.DiaSalida;
import java.awt.Cursor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import managers.ManagerRankDias;

/**
 *
 * @author nMoncho
 */
public class CargaRankDias extends javax.swing.JFrame {
    
    /** Creates new form CargaRankDias */
    public CargaRankDias() {
        initComponents();
        comboDias.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    DiaSalida dia = (DiaSalida) e.getItem();
                    CargaRankDias.this.textPuntaje.setText(Integer.toString(dia.getPuntos()));
                }
                if(e.getStateChange() == ItemEvent.DESELECTED){
                    DiaSalida dia = (DiaSalida) e.getItem();
                    try{
                        int puntaje = Integer.parseInt(CargaRankDias.this.textPuntaje.getText());
                        dia.setPuntos(new Integer(puntaje));
                    }catch(NumberFormatException ex){
                        JOptionPane.showMessageDialog(CargaRankDias.this, "Ingresate un puntaje incorrecto.\nPor favor, verifica y correji", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
                    }
                }
            }
        });

        cargarDias();
        this.setLocation(200, 200);
    }

    private void cargarDias(){
        DiaSalida[] dias = gestor.getDiasSalida();
        for(int i=0;i<dias.length;i++){
            comboDias.addItem(dias[i]);
            if(i == 0)
                this.textPuntaje.setText(Integer.toString(dias[i].getPuntos()));
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDia = new javax.swing.JLabel();
        comboDias = new javax.swing.JComboBox();
        labelPuntaje = new javax.swing.JLabel();
        textPuntaje = new javax.swing.JTextField();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Puntaje de Dias");
        setIconImage(new ImageIcon(getClass().getResource("/resources/arrow-branch-bgr-16-ns.png")).getImage());
        setResizable(false);

        labelDia.setText("Dia:");

        labelPuntaje.setText("Puntaje: ");

        textPuntaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textPuntajeActionPerformed(evt);
            }
        });

        botonAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-check-16-ns.png"))); // NOI18N
        botonAceptar.setText("Guardar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-cross-16-ns.png"))); // NOI18N
        botonCancelar.setText("Cerrar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelDia)
                                .addGap(45, 45, 45)
                                .addComponent(comboDias, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelPuntaje)
                                .addGap(18, 18, 18)
                                .addComponent(textPuntaje)))
                        .addGap(93, 93, 93))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botonAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(botonCancelar)
                        .addGap(24, 24, 24))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonAceptar, botonCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDia)
                    .addComponent(comboDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPuntaje)
                    .addComponent(textPuntaje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonCancelar))
                .addGap(22, 22, 22))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonAceptar, botonCancelar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        guardarPuntaje();
    }//GEN-LAST:event_botonAceptarActionPerformed

    private void textPuntajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textPuntajeActionPerformed
        guardarPuntaje();
    }//GEN-LAST:event_textPuntajeActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void guardarPuntaje(){
        int puntaje = 0;
        String sPuntaje = CargaRankDias.this.textPuntaje.getText();
        DiaSalida dia;
        //Validar puntaje
        try{
            puntaje = Integer.parseInt(sPuntaje);
            dia = (DiaSalida)comboDias.getSelectedItem();
            dia.setPuntos(puntaje);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            boolean exito = gestor.actualizarDiaSalidas();
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            
            if(exito)
                JOptionPane.showMessageDialog(null, "Los cambios se realizaron exitosamente", "Exito", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
            else
                JOptionPane.showMessageDialog(null, "No se pudieron realizar los cambios.\nPor favor, intentalo mas tarde.", "Fallido", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);

        }catch(NumberFormatException ex){
            CargaRankDias.this.textPuntaje.setText("");
            JOptionPane.showMessageDialog(CargaRankDias.this, "Ingresate un puntaje incorrecto.\nPor favor, verifica y correji", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargaRankDias().setVisible(true);
            }
        });
    }

    private ManagerRankDias gestor = new ManagerRankDias();

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JComboBox comboDias;
    private javax.swing.JLabel labelDia;
    private javax.swing.JLabel labelPuntaje;
    private javax.swing.JTextField textPuntaje;
    // End of variables declaration//GEN-END:variables

}
